package clientView;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Locale;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import object.SalaryBonus;
import objectView.EmSalarySheet;
import objectView.EmTimeSheet;
import objectView.EmployeeInfo;
import utility.DayMonthYear;
import utility.StringUtil;
import utility.TextConverter;
import clientMain.Login;
import clientMain.Main;
import clientUtilGUI.KeyValue;
import clientUtilGUI.UtilityGUI;

import com.toedter.calendar.JMonthChooser;
import com.toedter.calendar.JYearChooser;

public class SS_Calculate extends JScrollPane implements ActionListener{

	private JPanel panel;
	private JLabel lblTitle;
	private JLabel lblMonth;
	private JMonthChooser monthChooser;
	private JRadioButton rdbtnOption;
	private JLabel lblYear;
	private JYearChooser yearChooser;
	private JLabel lblEmId;
	private JTextField txtEmId;
	private JLabel lblError;
	private JLabel lblSB;
	private JComboBox cbbEMSB;
	private JLabel lblError2;
	private JButton btnCal;
	private JButton btnCancel;
	
	private JPanel panelResult;
	private JLabel lblTotalAM;
	private JLabel lbl_TotalAM;
	private JLabel lblSCAM;
	private JLabel lbl_SCAM;
	private JLabel lblSalaryAM;
	private JLabel lbl_SalaryAM;
	private JLabel lblTotalPM;
	private JLabel lbl_TotalPM;
	private JLabel lblSCPM;
	private JLabel lbl_SCPM;
	private JLabel lblSalaryPM;
	private JLabel lbl_SalaryPM;
	private JLabel lbl_SB;
	private JLabel lblPercent;
	private JLabel lbl_Percent;
	private JLabel lblTotalSalary;
	private JLabel lbl_TotalSalary;
	private JLabel lblSCYear;
	private JLabel lbl_SCYear;
	private JLabel lblSalaryYear;
	private JLabel lbl_SalaryYear;
	private JLabel lblResult;
	private JPanel panelMonthYear;
	private JLabel lblId;
	private JLabel lblEmName;
	private JLabel lbl_Id;
	private JLabel lbl_EmName;
	private GridBagConstraints gbc_result;
	
	private String monthYear;
	private String emId;
	private String emName;
	private int contractId;
	private int sbId;
	private boolean action = true;
	private boolean flag = true;
	private boolean check = false;
	private EmSalarySheet emSS;
	private JRadioButton rdbtnAdd;
	private JRadioButton rdbtnEdit;
	

	public SS_Calculate(){
		setViewportView(initComponent());
		setBorder(new EmptyBorder(0, 0, 0, 0));
	}
	
	
	private JPanel initComponent() {
		panel = new JPanel();
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};		
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		panel.setLayout(gbl_panel);
		
		lblTitle = new JLabel("Tính Lương");
		lblTitle.setFont(UtilityGUI.fontHeader1);
		lblTitle.setForeground(UtilityGUI.colorHeader1);
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridwidth = 5;
		gbc_lblTitle.insets = new Insets(10, 20, 5, 20);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		panel.add(lblTitle, gbc_lblTitle);
		
		rdbtnAdd = new JRadioButton("Thêm");
		rdbtnAdd.setFont(UtilityGUI.fontHeader3);
		rdbtnAdd.setForeground(UtilityGUI.colorHeader2);
		rdbtnAdd.setSelected(true);
		rdbtnAdd.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				AbstractButton aButton = (AbstractButton)e.getSource();
		        ButtonModel aModel = aButton.getModel();
		        boolean selected = aModel.isSelected();
		        if(selected){
		        	action = true;
		        }else{
		        	action = false;
		        }				
			}
		});
		GridBagConstraints gbc_rdbtnAdd = new GridBagConstraints();
		gbc_rdbtnAdd.anchor = GridBagConstraints.WEST;
		gbc_rdbtnAdd.insets = new Insets(5, 20, 5, 5);
		gbc_rdbtnAdd.gridx = 0;
		gbc_rdbtnAdd.gridy = 1;
		panel.add(rdbtnAdd, gbc_rdbtnAdd);
		
		rdbtnEdit = new JRadioButton("Sửa");
		rdbtnEdit.setFont(UtilityGUI.fontHeader2);
		rdbtnEdit.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_rdbtnEdit = new GridBagConstraints();
		gbc_rdbtnEdit.anchor = GridBagConstraints.WEST;
		gbc_rdbtnEdit.insets = new Insets(5, 0, 5, 5);
		gbc_rdbtnEdit.gridx = 1;
		gbc_rdbtnEdit.gridy = 1;
		panel.add(rdbtnEdit, gbc_rdbtnEdit);
		
		ButtonGroup groupModifier = new ButtonGroup();
		groupModifier.add(rdbtnAdd);
		groupModifier.add(rdbtnEdit);
		
		lblMonth = new JLabel("Tháng :");
		GridBagConstraints gbc_lblMonth = new GridBagConstraints();
		gbc_lblMonth.anchor = GridBagConstraints.WEST;
		gbc_lblMonth.insets = new Insets(3, 20, 5, 5);
		gbc_lblMonth.gridx = 0;
		gbc_lblMonth.gridy = 2;
		panel.add(lblMonth, gbc_lblMonth);
		lblMonth.setFont(UtilityGUI.fontLblForm);
		
		monthChooser = new JMonthChooser();
		monthChooser.setBorder(UIManager.getBorder("ComboBox.border"));
		monthChooser.setFont(UtilityGUI.fontComboBox);
		monthChooser.setLocale(new Locale("vi"));
		monthChooser.getComboBox().setPreferredSize(new Dimension(130, 22));
		GridBagConstraints gbc_monthChooser = new GridBagConstraints();
		gbc_monthChooser.anchor = GridBagConstraints.WEST;
		gbc_monthChooser.insets = new Insets(3, 0, 5, 5);
		gbc_monthChooser.gridx = 1;
		gbc_monthChooser.gridy = 2;
		panel.add(monthChooser, gbc_monthChooser);		
			
		rdbtnOption = new JRadioButton("Tháng 13(Tết)");
		GridBagConstraints gbc_rdbtnOption = new GridBagConstraints();
		gbc_rdbtnOption.anchor = GridBagConstraints.WEST;
		gbc_rdbtnOption.insets = new Insets(3, 8, 5, 5);
		gbc_rdbtnOption.gridx = 2;
		gbc_rdbtnOption.gridy = 2;
		panel.add(rdbtnOption, gbc_rdbtnOption);
		rdbtnOption.setFont(UtilityGUI.fontLblForm);
		rdbtnOption.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				AbstractButton aButton = (AbstractButton)e.getSource();
		        ButtonModel aModel = aButton.getModel();
		        boolean selected = aModel.isSelected();
		        if(selected){
		        	monthChooser.setEnabled(false);
		        }else{
		        	monthChooser.setEnabled(true);
		        }
		        lblError.setText("");
		        lblError2.setText("");
		        lblResult.setText("");
			}
		});
		
		lblYear = new JLabel("Năm :");
		lblYear.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblYear = new GridBagConstraints();
		gbc_lblYear.anchor = GridBagConstraints.WEST;
		gbc_lblYear.insets = new Insets(3, 8, 5, 5);
		gbc_lblYear.gridx = 3;
		gbc_lblYear.gridy = 2;
		panel.add(lblYear, gbc_lblYear);
		
		yearChooser = new JYearChooser();
		yearChooser.setEndYear(Main.fullday.getYear()+1900);
		yearChooser.setStartYear(2000);
		yearChooser.setFont(UtilityGUI.fontComboBox);
		GridBagConstraints gbc_yearChooser = new GridBagConstraints();
		gbc_yearChooser.anchor = GridBagConstraints.WEST;
		gbc_yearChooser.insets = new Insets(3, 0, 5, 20);
		gbc_yearChooser.gridx = 4;
		gbc_yearChooser.gridy = 2;
		panel.add(yearChooser, gbc_yearChooser);	
		
		lblEmId = new JLabel("Nhập Mã NV :");
		lblEmId.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmId = new GridBagConstraints();
		gbc_lblEmId.anchor = GridBagConstraints.WEST;
		gbc_lblEmId.insets = new Insets(3, 20, 5, 5);
		gbc_lblEmId.gridx = 0;
		gbc_lblEmId.gridy = 3;
		panel.add(lblEmId, gbc_lblEmId);
		
		txtEmId = new JTextField();
		txtEmId.addFocusListener(new FocusTxtEmId());
		GridBagConstraints gbc_txtEmId = new GridBagConstraints();
		gbc_txtEmId.fill = GridBagConstraints.BOTH;
		gbc_txtEmId.anchor = GridBagConstraints.WEST;
		gbc_txtEmId.insets = new Insets(3, 0, 5, 5);
		gbc_txtEmId.gridx = 1;
		gbc_txtEmId.gridy = 3;
		panel.add(txtEmId, gbc_txtEmId);
		
		lblError = new JLabel("");
		lblError.setFont(UtilityGUI.fontLblForm);
		lblError.setForeground(UtilityGUI.colorNote);
		GridBagConstraints gbc_lblError = new GridBagConstraints();
		gbc_lblError.anchor = GridBagConstraints.WEST;
		gbc_lblError.gridwidth = 3;
		gbc_lblError.insets = new Insets(3, 8, 5, 20);
		gbc_lblError.gridx = 2;
		gbc_lblError.gridy = 3;
		panel.add(lblError, gbc_lblError);
		
		lblSB = new JLabel("Mức Thưởng :");
		lblSB.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblSB = new GridBagConstraints();
		gbc_lblSB.anchor = GridBagConstraints.WEST;
		gbc_lblSB.insets = new Insets(3, 20, 5, 5);
		gbc_lblSB.gridx = 0;
		gbc_lblSB.gridy = 4;
		panel.add(lblSB, gbc_lblSB);
		
		cbbEMSB = new JComboBox();
		cbbEMSB.setEnabled(false);
		cbbEMSB.setFont(UtilityGUI.fontComboBox);
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.fill = GridBagConstraints.BOTH;
		gbc_comboBox.anchor = GridBagConstraints.WEST;
		gbc_comboBox.gridwidth = 2;
		gbc_comboBox.insets = new Insets(3, 0, 5, 5);
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 4;
		panel.add(cbbEMSB, gbc_comboBox);
		
		lblError2 = new JLabel("");
		lblError2.setFont(UtilityGUI.fontLblForm);
		lblError2.setForeground(UtilityGUI.colorNote);
		GridBagConstraints gbc_lblError2 = new GridBagConstraints();
		gbc_lblError2.anchor = GridBagConstraints.WEST;
		gbc_lblError2.gridwidth = 2;
		gbc_lblError2.insets = new Insets(3, 8, 5, 20);
		gbc_lblError2.gridx = 3;
		gbc_lblError2.gridy = 4;
		panel.add(lblError2, gbc_lblError2);
		
		btnCal = new JButton(UtilityGUI.btnCal);
		btnCal.setFont(UtilityGUI.fontBtnForm);
		btnCal.addActionListener(this);
		GridBagConstraints gbc_btnCal = new GridBagConstraints();
		gbc_btnCal.anchor = GridBagConstraints.EAST;
		gbc_btnCal.insets = new Insets(3, 0, 5, 5);
		gbc_btnCal.gridx = 1;
		gbc_btnCal.gridy = 5;
		panel.add(btnCal, gbc_btnCal);
		
		btnCancel = new JButton(UtilityGUI.btnCancel);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		btnCancel.addActionListener(this);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.anchor = GridBagConstraints.WEST;
		gbc_btnCancel.insets = new Insets(3, 8, 5, 5);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 5;
		panel.add(btnCancel, gbc_btnCancel);
		
		lblResult = new JLabel("");
		lblResult.setFont(UtilityGUI.fontLblForm);
		lblResult.setForeground(UtilityGUI.colorNote);
		gbc_result = new GridBagConstraints();
		gbc_result.fill = GridBagConstraints.HORIZONTAL;
		gbc_result.anchor = GridBagConstraints.NORTHWEST;
		gbc_result.gridwidth = 5;
		gbc_result.insets = new Insets(10, 20, 10, 20);
		gbc_result.gridx = 0;
		gbc_result.gridy = 6;
		panel.add(lblResult, gbc_result);
		
		return panel;
	}
	
	private JPanel getResult(EmSalarySheet emSS){
		panelResult = new JPanel();
		GridBagLayout gbl_panelResult = new GridBagLayout();
		gbl_panelResult.columnWidths = new int[]{0, 0, 0, 0};
		gbl_panelResult.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		gbl_panelResult.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panelResult.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		panelResult.setLayout(gbl_panelResult);
		
		panelMonthYear = UtilityGUI.titleForm("Tháng : "+ monthYear);
		GridBagConstraints gbc_panelMonthYear = new GridBagConstraints();
		gbc_panelMonthYear.fill = GridBagConstraints.BOTH;
		gbc_panelMonthYear.anchor = GridBagConstraints.WEST;
		gbc_panelMonthYear.insets = new Insets(0, 0, 10, 0);
		gbc_panelMonthYear.gridwidth = 4;
		gbc_panelMonthYear.gridx = 0;
		gbc_panelMonthYear.gridy = 0;
		panelResult.add(panelMonthYear, gbc_panelMonthYear);
		
		lblId = new JLabel("Mã Nhân Viên :");
		lblId.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblId = new GridBagConstraints();
		gbc_lblId.anchor = GridBagConstraints.WEST;
		gbc_lblId.insets = new Insets(3, 0, 5, 5);
		gbc_lblId.gridx = 0;
		gbc_lblId.gridy = 1;
		panelResult.add(lblId, gbc_lblId);
		
		lbl_Id = new JLabel(emId);
		lbl_Id.setFont(UtilityGUI.fontLblForm);
		lbl_Id.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lbl_Id = new GridBagConstraints();
		gbc_lbl_Id.anchor = GridBagConstraints.WEST;
		gbc_lbl_Id.insets = new Insets(3, 0, 5, 5);
		gbc_lbl_Id.gridx = 1;
		gbc_lbl_Id.gridy = 1;
		panelResult.add(lbl_Id, gbc_lbl_Id);
		
		lblEmName = new JLabel("Họ Tên NV :");
		lblEmName.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblEmName = new GridBagConstraints();
		gbc_lblEmName.anchor = GridBagConstraints.WEST;
		gbc_lblEmName.insets = new Insets(3, 5, 5, 5);
		gbc_lblEmName.gridx = 2;
		gbc_lblEmName.gridy = 1;
		panelResult.add(lblEmName, gbc_lblEmName);
		
		lbl_EmName = new JLabel(emName);
		lbl_EmName.setFont(UtilityGUI.fontLblForm);
		lbl_EmName.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lbl_EmName = new GridBagConstraints();
		gbc_lbl_EmName.anchor = GridBagConstraints.WEST;
		gbc_lbl_EmName.insets = new Insets(3, 0, 5, 5);
		gbc_lbl_EmName.gridx = 3;
		gbc_lbl_EmName.gridy = 1;
		panelResult.add(lbl_EmName, gbc_lbl_EmName);
		
		if(!monthYear.contains("13")){
			lblTotalAM = new JLabel("Tổng Số Ngày Làm Sáng :");
			lblTotalAM.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblTotalAM = new GridBagConstraints();
			gbc_lblTotalAM.anchor = GridBagConstraints.WEST;
			gbc_lblTotalAM.insets = new Insets(10, 0, 5, 5);
			gbc_lblTotalAM.gridwidth = 2;
			gbc_lblTotalAM.gridx = 0;
			gbc_lblTotalAM.gridy = 2;
			panelResult.add(lblTotalAM, gbc_lblTotalAM);
			
			lbl_TotalAM = new JLabel(emSS.getTotalAM());
			lbl_TotalAM.setFont(UtilityGUI.fontLblForm);
			lbl_TotalAM.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_TotalAM = new GridBagConstraints();
			gbc_lbl_TotalAM.anchor = GridBagConstraints.WEST;
			gbc_lbl_TotalAM.insets = new Insets(10, 5, 5, 5);
			gbc_lbl_TotalAM.gridwidth = 2;
			gbc_lbl_TotalAM.gridx = 2;
			gbc_lbl_TotalAM.gridy = 2;
			panelResult.add(lbl_TotalAM, gbc_lbl_TotalAM);
			
			lblSCAM = new JLabel("Hệ Số Lương Ca Sáng :");
			lblSCAM.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSCAM = new GridBagConstraints();
			gbc_lblSCAM.insets = new Insets(3, 0, 5, 5);
			gbc_lblSCAM.anchor = GridBagConstraints.WEST;
			gbc_lblSCAM.gridwidth = 2;
			gbc_lblSCAM.gridx = 0;
			gbc_lblSCAM.gridy = 3;
			panelResult.add(lblSCAM, gbc_lblSCAM);
			
			lbl_SCAM = new JLabel(emSS.getScAM());
			lbl_SCAM.setFont(UtilityGUI.fontLblForm);
			lbl_SCAM.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SCAM = new GridBagConstraints();
			gbc_lbl_SCAM.anchor = GridBagConstraints.WEST;
			gbc_lbl_SCAM.insets = new Insets(3, 5, 5, 5);
			gbc_lbl_SCAM.gridwidth = 2;
			gbc_lbl_SCAM.gridx = 2;
			gbc_lbl_SCAM.gridy = 3;
			panelResult.add(lbl_SCAM, gbc_lbl_SCAM);
			
			lblSalaryAM = new JLabel("Tổng Lương Ca Sáng :");
			lblSalaryAM.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSalaryAM = new GridBagConstraints();
			gbc_lblSalaryAM.anchor = GridBagConstraints.WEST;
			gbc_lblSalaryAM.insets = new Insets(3, 0, 5, 5);
			gbc_lblSalaryAM.gridwidth = 2;
			gbc_lblSalaryAM.gridx = 0;
			gbc_lblSalaryAM.gridy = 4;
			panelResult.add(lblSalaryAM, gbc_lblSalaryAM);
			
			lbl_SalaryAM = new JLabel(emSS.getSalaryAM());
			lbl_SalaryAM.setFont(UtilityGUI.fontLblForm);
			lbl_SalaryAM.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SalaryAM = new GridBagConstraints();
			gbc_lbl_SalaryAM.anchor = GridBagConstraints.WEST;
			gbc_lbl_SalaryAM.insets = new Insets(3, 5, 5, 5);
			gbc_lbl_SalaryAM.gridwidth = 2;
			gbc_lbl_SalaryAM.gridx = 2;
			gbc_lbl_SalaryAM.gridy = 4;
			panelResult.add(lbl_SalaryAM, gbc_lbl_SalaryAM);
			
			lblTotalPM = new JLabel("Tổng Số Ngày Làm Đêm :");
			lblTotalPM.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblTotalPM = new GridBagConstraints();
			gbc_lblTotalPM.anchor = GridBagConstraints.WEST;
			gbc_lblTotalPM.insets = new Insets(8, 0, 5, 5);
			gbc_lblTotalPM.gridwidth = 2;
			gbc_lblTotalPM.gridx = 0;
			gbc_lblTotalPM.gridy = 5;
			panelResult.add(lblTotalPM, gbc_lblTotalPM);
			
			lbl_TotalPM = new JLabel(emSS.getTotalPM());
			lbl_TotalPM.setFont(UtilityGUI.fontLblForm);
			lbl_TotalPM.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_TotalPM = new GridBagConstraints();
			gbc_lbl_TotalPM.anchor = GridBagConstraints.WEST;
			gbc_lbl_TotalPM.insets = new Insets(8, 5, 5, 5);
			gbc_lbl_TotalPM.gridwidth = 2;
			gbc_lbl_TotalPM.gridx = 2;
			gbc_lbl_TotalPM.gridy = 5;
			panelResult.add(lbl_TotalPM, gbc_lbl_TotalPM);
			
			lblSCPM = new JLabel("Hệ Số Lương Ca Đêm :");
			lblSCPM.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSCPM = new GridBagConstraints();
			gbc_lblSCPM.anchor = GridBagConstraints.WEST;
			gbc_lblSCPM.insets = new Insets(3, 0, 5, 5);
			gbc_lblSCPM.gridwidth = 2;
			gbc_lblSCPM.gridx = 0;
			gbc_lblSCPM.gridy = 6;
			panelResult.add(lblSCPM, gbc_lblSCPM);
			
			lbl_SCPM = new JLabel(emSS.getScPM());
			lbl_SCPM.setFont(UtilityGUI.fontLblForm);
			lbl_SCPM.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SCPM = new GridBagConstraints();
			gbc_lbl_SCPM.anchor = GridBagConstraints.WEST;
			gbc_lbl_SCPM.insets = new Insets(3, 5, 5, 5);
			gbc_lbl_SCPM.gridwidth = 2;
			gbc_lbl_SCPM.gridx = 2;
			gbc_lbl_SCPM.gridy = 6;
			panelResult.add(lbl_SCPM, gbc_lbl_SCPM);
			
			lblSalaryPM = new JLabel("Tổng Lương Ca Đêm :");
			lblSalaryPM.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSalaryPM = new GridBagConstraints();
			gbc_lblSalaryPM.insets = new Insets(3, 0, 5, 5);
			gbc_lblSalaryPM.anchor = GridBagConstraints.WEST;
			gbc_lblSalaryPM.gridwidth = 2;
			gbc_lblSalaryPM.gridx = 0;
			gbc_lblSalaryPM.gridy = 7;
			panelResult.add(lblSalaryPM, gbc_lblSalaryPM);
			
			lbl_SalaryPM = new JLabel(emSS.getSalaryPM());
			lbl_SalaryPM.setFont(UtilityGUI.fontLblForm);
			lbl_SalaryPM.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SalaryPM = new GridBagConstraints();
			gbc_lbl_SalaryPM.anchor = GridBagConstraints.WEST;
			gbc_lbl_SalaryPM.insets = new Insets(3, 5, 5, 5);
			gbc_lbl_SalaryPM.gridwidth = 2;
			gbc_lbl_SalaryPM.gridx = 2;
			gbc_lbl_SalaryPM.gridy = 7;
			panelResult.add(lbl_SalaryPM, gbc_lbl_SalaryPM);
			
			lblSB = new JLabel("Thưởng :");
			lblSB.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSB = new GridBagConstraints();
			gbc_lblSB.insets = new Insets(8, 0, 5, 5);
			gbc_lblSB.anchor = GridBagConstraints.WEST;
			gbc_lblSB.gridwidth = 2;
			gbc_lblSB.gridx = 0;
			gbc_lblSB.gridy = 8;
			panelResult.add(lblSB, gbc_lblSB);
			
			lbl_SB = new JLabel(emSS.getSb());
			lbl_SB.setFont(UtilityGUI.fontLblForm);
			lbl_SB.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SB = new GridBagConstraints();
			gbc_lbl_SB.anchor = GridBagConstraints.WEST;
			gbc_lbl_SB.insets = new Insets(8, 5, 5, 5);
			gbc_lbl_SB.gridwidth = 2;
			gbc_lbl_SB.gridx = 2;
			gbc_lbl_SB.gridy = 8;
			panelResult.add(lbl_SB, gbc_lbl_SB);
			
			lblPercent = new JLabel("Mức Lương Hiện Tại :");
			lblPercent.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblPercent = new GridBagConstraints();
			gbc_lblPercent.insets = new Insets(8, 0, 5, 5);
			gbc_lblPercent.anchor = GridBagConstraints.WEST;
			gbc_lblPercent.gridwidth = 2;
			gbc_lblPercent.gridx = 0;
			gbc_lblPercent.gridy = 9;
			panelResult.add(lblPercent, gbc_lblPercent);
			
			lbl_Percent = new JLabel(emSS.getContractBenefit()+" lương chính thức");
			lbl_Percent.setFont(UtilityGUI.fontLblForm);
			lbl_Percent.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_Percent = new GridBagConstraints();
			gbc_lbl_Percent.anchor = GridBagConstraints.WEST;
			gbc_lbl_Percent.insets = new Insets(8, 5, 5, 5);
			gbc_lbl_Percent.gridwidth = 2;
			gbc_lbl_Percent.gridx = 2;
			gbc_lbl_Percent.gridy = 9;
			panelResult.add(lbl_Percent, gbc_lbl_Percent);
			
			lblTotalSalary = new JLabel("Tổng Lương :");
			lblTotalSalary.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblTotalSalary = new GridBagConstraints();
			gbc_lblTotalSalary.insets = new Insets(8, 0, 0, 5);
			gbc_lblTotalSalary.anchor = GridBagConstraints.NORTHWEST;
			gbc_lblTotalSalary.gridwidth = 2;
			gbc_lblTotalSalary.gridx = 0;
			gbc_lblTotalSalary.gridy = 10;
			panelResult.add(lblTotalSalary, gbc_lblTotalSalary);
			
			lbl_TotalSalary = new JLabel(emSS.getTotalSalary());
			lbl_TotalSalary.setFont(UtilityGUI.fontLblForm);
			lbl_TotalSalary.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_TotalSalary = new GridBagConstraints();
			gbc_lbl_TotalSalary.anchor = GridBagConstraints.NORTHWEST;
			gbc_lbl_TotalSalary.insets = new Insets(8, 5, 0, 5);
			gbc_lbl_TotalSalary.gridwidth = 2;
			gbc_lbl_TotalSalary.gridx = 2;
			gbc_lbl_TotalSalary.gridy = 10;
			panelResult.add(lbl_TotalSalary, gbc_lbl_TotalSalary);
			
		}else{
			lblSCYear = new JLabel("Hệ Số Lương Tết:");
			lblSCYear.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSCYear = new GridBagConstraints();
			gbc_lblSCYear.anchor = GridBagConstraints.WEST;
			gbc_lblSCYear.insets = new Insets(10, 0, 5, 5);
			gbc_lblSCYear.gridwidth = 2;
			gbc_lblSCYear.gridx = 0;
			gbc_lblSCYear.gridy = 2;
			panelResult.add(lblSCYear, gbc_lblSCYear);
			
			lbl_SCYear = new JLabel(emSS.getScYear());
			lbl_SCYear.setFont(UtilityGUI.fontLblForm);
			lbl_SCYear.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SCYear = new GridBagConstraints();
			gbc_lbl_SCYear.anchor = GridBagConstraints.WEST;
			gbc_lbl_SCYear.insets = new Insets(10, 5, 5, 5);
			gbc_lbl_SCYear.gridwidth = 2;
			gbc_lbl_SCYear.gridx = 2;
			gbc_lbl_SCYear.gridy = 2;
			panelResult.add(lbl_SCYear, gbc_lbl_SCYear);
			
			lblPercent = new JLabel("Mức Lương Hiện Tại :");
			lblPercent.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblPercent = new GridBagConstraints();
			gbc_lblPercent.insets = new Insets(8, 0, 5, 5);
			gbc_lblPercent.anchor = GridBagConstraints.WEST;
			gbc_lblPercent.gridwidth = 2;
			gbc_lblPercent.gridx = 0;
			gbc_lblPercent.gridy = 3;
			panelResult.add(lblPercent, gbc_lblPercent);
			
			lbl_Percent = new JLabel(emSS.getContractBenefit()+" lương chính thức");
			lbl_Percent.setFont(UtilityGUI.fontLblForm);
			lbl_Percent.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_Percent = new GridBagConstraints();
			gbc_lbl_Percent.anchor = GridBagConstraints.WEST;
			gbc_lbl_Percent.insets = new Insets(8, 5, 5, 5);
			gbc_lbl_Percent.gridwidth = 2;
			gbc_lbl_Percent.gridx = 2;
			gbc_lbl_Percent.gridy = 3;
			panelResult.add(lbl_Percent, gbc_lbl_Percent);
			
			lblSalaryYear = new JLabel("Lương Tết :");
			lblSalaryYear.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSalaryYear = new GridBagConstraints();
			gbc_lblSalaryYear.insets = new Insets(8, 0, 5, 5);
			gbc_lblSalaryYear.anchor = GridBagConstraints.WEST;
			gbc_lblSalaryYear.gridwidth = 2;
			gbc_lblSalaryYear.gridx = 0;
			gbc_lblSalaryYear.gridy = 4;
			panelResult.add(lblSalaryYear, gbc_lblSalaryYear);
			
			lbl_SalaryYear = new JLabel(emSS.getSalaryYear());
			lbl_SalaryYear.setFont(UtilityGUI.fontLblForm);
			lbl_SalaryYear.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SalaryYear = new GridBagConstraints();
			gbc_lbl_SalaryYear.anchor = GridBagConstraints.WEST;
			gbc_lbl_SalaryYear.insets = new Insets(8, 5, 5, 5);
			gbc_lbl_SalaryYear.gridwidth = 2;
			gbc_lbl_SalaryYear.gridx = 2;
			gbc_lbl_SalaryYear.gridy = 4;
			panelResult.add(lbl_SalaryYear, gbc_lbl_SalaryYear);
			
			lblSB = new JLabel("Thưởng :");
			lblSB.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblSB = new GridBagConstraints();
			gbc_lblSB.insets = new Insets(8, 0, 5, 5);
			gbc_lblSB.anchor = GridBagConstraints.WEST;
			gbc_lblSB.gridwidth = 2;
			gbc_lblSB.gridx = 0;
			gbc_lblSB.gridy = 5;
			panelResult.add(lblSB, gbc_lblSB);
			
			lbl_SB = new JLabel(emSS.getSb());
			lbl_SB.setFont(UtilityGUI.fontLblForm);
			lbl_SB.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_SB = new GridBagConstraints();
			gbc_lbl_SB.anchor = GridBagConstraints.WEST;
			gbc_lbl_SB.insets = new Insets(8, 5, 5, 5);
			gbc_lbl_SB.gridwidth = 2;
			gbc_lbl_SB.gridx = 2;
			gbc_lbl_SB.gridy = 5;
			panelResult.add(lbl_SB, gbc_lbl_SB);
			
			lblTotalSalary = new JLabel("Tổng Lương :");
			lblTotalSalary.setFont(UtilityGUI.fontHeader3);
			GridBagConstraints gbc_lblTotalSalary = new GridBagConstraints();
			gbc_lblTotalSalary.insets = new Insets(8, 0, 0, 5);
			gbc_lblTotalSalary.anchor = GridBagConstraints.NORTHWEST;
			gbc_lblTotalSalary.gridwidth = 2;
			gbc_lblTotalSalary.gridx = 0;
			gbc_lblTotalSalary.gridy = 6;
			panelResult.add(lblTotalSalary, gbc_lblTotalSalary);
			
			lbl_TotalSalary = new JLabel(emSS.getTotalSalary());
			lbl_TotalSalary.setFont(UtilityGUI.fontLblForm);
			lbl_TotalSalary.setForeground(UtilityGUI.colorLblForm);
			GridBagConstraints gbc_lbl_TotalSalary = new GridBagConstraints();
			gbc_lbl_TotalSalary.anchor = GridBagConstraints.NORTHWEST;
			gbc_lbl_TotalSalary.insets = new Insets(8, 5, 0, 5);
			gbc_lbl_TotalSalary.gridwidth = 2;
			gbc_lbl_TotalSalary.gridx = 2;
			gbc_lbl_TotalSalary.gridy = 6;
			panelResult.add(lbl_TotalSalary, gbc_lbl_TotalSalary);
		}
		return panelResult;
	}


	private class FocusTxtEmId extends FocusAdapter{
		public void focusLost(FocusEvent e) {
			emId = StringUtil.removeUnwantedSpace(txtEmId.getText());
			txtEmId.setText(emId);
			if(!StringUtil.isValidEmId(emId)){
				lblError.setText(UtilityGUI.errorValidEmId);
				flag = false;
			}else{
				Vector<EmployeeInfo> list = Main.viewEm.searchEm(1 , "Em_Id", emId);
				if(list == null || list.size() == 0){
					lblError.setText(UtilityGUI.errorExistsEmId);
					flag = false;
				}else{
					boolean perm = false;
					for(int i=0; i<Login.listAuthorization.size();i++){
						if(Login.listAuthorization.get(i).getModuleDep() == list.get(0).getDepId()){
							perm = true;
						}
					}
					if(perm == false){
						lblError.setText(UtilityGUI.errorAuth);
						flag = false;
					}else {
						emId = list.get(0).getEmId();
						emName = list.get(0).getEmFullName();
						contractId = list.get(0).getEmContract();
						lblError.setText("");
						Vector<SalaryBonus> listSB = Main.actionSB.listEMSB(emId);
						cbbEMSB.removeAllItems();
						cbbEMSB.addItem("-- Chọn Loại Tiền Thưởng --");
						for(int i=0; i<listSB.size(); i++){
							String id = Integer.toString(listSB.get(i).getsBId());
							String value = listSB.get(i).getsB_Name();
							cbbEMSB.addItem(new KeyValue(id, value));
						}
						cbbEMSB.setEnabled(true);
						cbbEMSB.requestFocusInWindow();
						flag = true;
					}
				}
			}
		}
		
		public void focusGained(FocusEvent e) {
			lblError.setText("");
			lblResult.setText("");
		}
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnCal){
			lblResult.setText("");
			if(panelResult != null){
				panelResult.setVisible(false);
				panel.remove(panelResult);
				panelResult.removeAll();
				emSS = null;
				lblResult.setVisible(true);
			}
			
			if(rdbtnOption.isSelected()){
				if((Main.fullday.getMonth() == 11 && yearChooser.getYear() == Main.fullday.getYear()+1900) || 
						(Main.fullday.getMonth() <2 && yearChooser.getYear()+1 == Main.fullday.getYear()+1900)){
					monthYear = DayMonthYear.monthYear(13, yearChooser.getYear());
				}else{
					lblResult.setText("Ko phải thời điểm tính lương Tết "+yearChooser.getYear()+"!");
					return;
				}	
			}else{
				if(monthChooser.getMonth() > Main.fullday.getMonth() 
						&& yearChooser.getYear() == Main.fullday.getYear()+1900){
					lblResult.setText("Ko chọn thời điểm tương lai!!!");
					monthChooser.getComboBox().requestFocusInWindow();
					return;
				}else if(monthChooser.getMonth()+1 == Main.fullday.getMonth()
							&& yearChooser.getYear() == Main.fullday.getYear()+1900
							&& Main.fullday.getDate() <= 10 && Main.fullday.getDate() >5){
					monthYear = DayMonthYear.monthYear(monthChooser.getMonth()+1, yearChooser.getYear());
				}else{
					monthYear = DayMonthYear.monthYear(monthChooser.getMonth()+1, yearChooser.getYear());
					lblResult.setText("Ko phải thời điểm tính lương tháng "+monthYear+" !");
					monthChooser.getComboBox().requestFocusInWindow();
					monthYear = null;
					return;
				}
			}
			
			if(txtEmId.getText().isEmpty()){
				lblError.setText(UtilityGUI.errorValidEmId);
				return;
			}
			
			if(cbbEMSB.getSelectedIndex() == 0){
				lblError2.setText("Hãy lựa chọn mức thưởng !");
				cbbEMSB.requestFocusInWindow();
				return;
			}else{
				String sbName = ((KeyValue)cbbEMSB.getSelectedItem()).getValue();
				sbName = TextConverter.toEnglish(sbName).toLowerCase();
				if(!monthYear.contains("13")){
					if(sbName.contains("nam") || sbName.contains("tet")){
						lblError2.setText("Không lựa chọn mức thưởng Tết !");
						return;
					}
				}else{
					if(!(sbName.contains("nam") || sbName.contains("tet") || 
							sbName.contains("khong") || sbName.contains("ko") || sbName.contains("0"))){
						lblError2.setText("Hãy lựa chọn thưởng Tết hoặc không có thưởng!");
						return;
					}
				}
				sbId = Integer.parseInt(((KeyValue)cbbEMSB.getSelectedItem()).getKey());
			}
			
			if(flag == true){
				lblError.setText("");
				lblError2.setText("");
				Vector<EmTimeSheet> emTS = Main.viewEm.getEmTimeSheet(monthYear, emId);
			    if(emTS == null || emTS.size() == 0){
			    	lblResult.setText(emId+" không có bảng chấm công tháng: "+monthYear+" !");
			    	return;
			    }
				check = Main.actionSS.checkExistEMSS(monthYear, emId);
				String start = "";
				String end = "";
				if(check == true){
					if(action == true){
						if(!monthYear.contains("13")){
							String[] listDay = DayMonthYear.listDaysofMonth(monthYear);
							start = listDay[0];
							end = listDay[listDay.length-1];
						}
						boolean cal = Main.actionSS.calculateSS(start, end, monthYear, emId, contractId, sbId, action);
						if(cal){
							lblResult.setVisible(false);
							emSS = Main.viewEm.getEmSalarySheet(monthYear, emId);
							panel.add(getResult(emSS), gbc_result);
						}else{
							lblResult.setText(UtilityGUI.errorAction);
							return;
						}
					}else{
						lblResult.setText(emId+" chưa có lương tháng: "+monthYear+" ! Hãy thêm bảng lương tháng "+monthYear+".");
						return;
					}
					
					
				}else{
					if(action == true){
						lblResult.setText(emId+" đã có lương tháng: "+monthYear+" !");
						monthChooser.getComboBox().requestFocusInWindow();
						return;
					}else{
						if(!monthYear.contains("13")){
							String[] listDay = DayMonthYear.listDaysofMonth(monthYear);
							start = listDay[0];
							end = listDay[listDay.length-1];
						}
						boolean cal = Main.actionSS.calculateSS(start, end, monthYear, emId, contractId, sbId, action);
						if(cal){
							lblResult.setVisible(false);
							emSS = Main.viewEm.getEmSalarySheet(monthYear, emId);
							panel.add(getResult(emSS), gbc_result);
						}else{
							lblResult.setText(UtilityGUI.errorAction);
							return;
						}
					}
				}
			}else return;
		}
		
		if(e.getSource() == btnCancel){
			Main.jTabbedPane.remove(this);
		}
	}

}
